package zdc.display {
import com.greensock.TweenLite;

import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Point;

/**
 *
 * @author Metal Slime
 */
public class BlockText extends MovieClip {
    //自动播放
    private var _autoStart:Boolean = true;
    //每个缓动的间隔
    private var _delay:Number = 0.3;
    //缓动时间
    private var _time:Number = 1;
    //起点
    private var _sx:Number = 300;
    private var _sy:Number = 400;
    private var _dir:String = 'v';

    private var _tw:TweenLite;
    private var _org_poses:Vector.<Point> = new Vector.<Point>();
    private var _ts:Vector.<DisplayObject> = new Vector.<DisplayObject>();
    private var _tws:Vector.<TweenLite> = new Vector.<TweenLite>();

    public function BlockText() {
        stage ? init() : addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        initVars();
        initState();
        initEvents();
        run();
    }

    private function initVars():void {
        for (var i:int = 0; i < this.numChildren; i++) {
            var dio:DisplayObject = this.getChildAt(i);
            _org_poses.push(new Point(dio.x, dio.y));
            _ts.push(dio);
        }
    }

    private function initState():void {
        resetAllStates();
    }

    private function initEvents():void {
        addEventListener(Event.EXIT_FRAME, onExitFrame);
    }

    private function onExitFrame(e:Event):void {
        removeEventListener(Event.EXIT_FRAME, onExitFrame);
        if (_autoStart) {
            playTxt();
        }
    }

    //播放
    private function playTxt():void {
        stopTxt();
        for (var i:int = 0; i < _ts.length; i++) {
            var t:DisplayObject = _ts[i];
            var pos:Point = _org_poses[i];
            t.visible = true;
            var tw:TweenLite = TweenLite.to(t, _time, {
                x: isH ? pos.x : t.x,
                y: isV ? pos.y : t.y,
                alpha: 1,
                delay: i * delay
            });
            _tws.push(_tw);
        }
    }

    //停止
    private function stopTxt():void {
        clearTws();
        resetAllStates();
    }

    private function clearTws():void {
        for each(var tw:TweenLite in _tws) {
            tw && _tw.kill();
        }
        _tws = new Vector.<TweenLite>();
    }


    private function resetState(t:DisplayObject):void {
        if (isV) {
            t.y = _sy;
        }
        if (isH) {
            t.x = _sx;
        }
        t.alpha = 0;
        t.visible = false;
    }

    private function resetAllStates():void {
        for (var i:int = 0; i < _ts.length; i++) {
            resetState(_ts[i]);
        }
    }

    private function run():void {

    }

    public function get autoStart():Boolean {
        return _autoStart;
    }

    [Inspectable(defaultValue=true)]
    public function set autoStart(value:Boolean):void {
        _autoStart = value;
    }

    public function get time():Number {
        return _time;
    }

    [Inspectable(defaultValue=1)]
    public function set time(value:Number):void {
        _time = value;
    }

    public function get delay():Number {
        return _delay;
    }

    [Inspectable(defaultValue=0.3)]
    public function set delay(value:Number):void {
        _delay = value;
    }

    public function get sx():Number {
        return _sx;
    }

    [Inspectable(defaultValue=300)]
    public function set sx(value:Number):void {
        _sx = value;
    }

    public function get sy():Number {
        return _sy;
    }

    [Inspectable(defaultValue=400)]
    public function set sy(value:Number):void {
        _sy = value;
    }

    public function get dir():String {
        return _dir;
    }

    [Inspectable(enumeration="h,v,hv", defaultValue="v")]
    public function set dir(value:String):void {
        _dir = value;
    }

    /**
     * 是否垂直方向移动
     */
    public function get isV():Boolean {
        return _dir.indexOf('v') > -1;
    }

    /**
     * 是否水平方向移动
     */
    public function get isH():Boolean {
        return _dir.indexOf('H') > -1;
    }
}
}